<template>
  <div class="app-container">
    <el-row :gutter="20" class="c-width c-flex c-hidden c-dis-flex-row">

    <el-col :span="20" :xs="24" class="c-dis-flex-col c-width c-hidden">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="车牌号码" prop="carCode">
          <el-input
            v-model="queryParams.carCode"
            placeholder="车牌号码"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

          <el-form-item label="排放阶段" prop="emissionLevel">
            <el-select v-model="queryParams.emissionLevel" placeholder="请选择排放阶段">
              <el-option
                v-for="dict in dict.type.emission_level"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>

          </el-form-item>

        <el-form-item label="注册时间" prop="dateRange">
          <el-date-picker
            v-model="dateRange"
            type="datetimerange"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            :default-time="['06:00:00', '23:59:59']"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>

      </el-form>

      <el-row :gutter="10" class="mb8 com_table_btn">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                     v-hasPermi="['transport:vehicles:add']">新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                     v-hasPermi="['transport:vehicles:remove']">删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="info"
            plain
            icon="el-icon-upload2"
            size="mini"
            @click="handleImport"
            v-hasPermi="['project:base:import']"
          >导入</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                     v-hasPermi="['transport:vehicles:export']">导出
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table  border size="small" height="100%" class="com_table"  :data="baseList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="车牌号" align="center" key="carCode" prop="carCode" >
        </el-table-column>
        <el-table-column label="燃料类型" align="center" key="fuelType" prop="fuelType"  >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.fuel_type" :value="scope.row.fuelType"/>
          </template>
        </el-table-column>
        <el-table-column label="车辆识别码" align="center" prop="vin" min-width="150" />
        <el-table-column label="发动机编号" align="center"  prop="engineCode"  min-width="150" />
        <el-table-column label="使用性质" align="center" key="usingProperty" prop="usingProperty"  >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.using_property" :value="scope.row.usingProperty"/>
          </template>
        </el-table-column>
        <el-table-column label="地址" align="center"  prop="addr" />
        <el-table-column label="所有人" align="center"  prop="userName" min-width="120" />
        <el-table-column label="品牌" align="center"  prop="brandName" min-width="120" />
        <el-table-column label="注册时间" align="center"  prop="registerTime" min-width="120" />
        <el-table-column label="排放阶段" align="center" key="emissionLevel" prop="emissionLevel"  >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.emission_level" :value="scope.row.emissionLevel"/>
          </template>
        </el-table-column>
        <el-table-column label="审核与否" align="center" key="auditStatus" prop="auditStatus"  >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.audit_status" :value="scope.row.auditStatus"/>
          </template>
        </el-table-column>

        <el-table-column label="行驶证照片" align="center"  prop="licenseImgUrl" min-width="120" />
        <el-table-column label="随车清单照片" align="center"  prop="otherImgUrl" min-width="120" />
        <el-table-column
          label="操作"
          align="center"
          width="160"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope" v-if="scope.row.userId !== 1">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['project:base:detail:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['project:base:detail:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

    </el-col>
    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">

        <el-col :span="12">
          <el-form-item label="车牌号" prop="carCode">
            <el-input v-model="form.carCode" placeholder="请输入车牌号" maxlength="30" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="燃料类型" prop="fuelType">
            <el-select v-model="form.fuelType" placeholder="请选择燃料类型">
              <el-option
                v-for="dict in dict.type.fuel_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="车辆识别码" prop="vin">
            <el-input v-model="form.vin" placeholder="请输入车辆识别码" maxlength="30" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="发动机编码" prop="engineCode">
            <el-input v-model="form.engineCode" placeholder="请输入发动机编码" maxlength="30" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="使用性质" prop="fuelType">
            <el-select v-model="form.usingProperty" placeholder="请选择使用性质">
              <el-option
                v-for="dict in dict.type.using_property"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>


        <el-col :span="12">
          <el-form-item label="地址" prop="addr">
            <el-input v-model="form.addr" placeholder="请输入地址" maxlength="30" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="所有人" prop="userName">
            <el-input v-model="form.userName" placeholder="请输入所有人" maxlength="30" />
          </el-form-item>
        </el-col>


        <el-col :span="12">
          <el-form-item label="品牌名称" prop="brandName">
            <el-input v-model="form.brandName" placeholder="请输入品牌名称" maxlength="30" />
          </el-form-item>
        </el-col>


        <el-col :span="12">
          <el-form-item label="排放阶段" prop="emissionLevel">
            <el-select v-model="form.emissionLevel" placeholder="请选择排放阶段">
              <el-option
                v-for="dict in dict.type.emission_level"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


      <!-- 导入对话框 -->
      <el-dialog class="dialog_small" :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
        <div class="dialog_small_cont">
          <el-upload
            class="c-width c-dis-flex-col"
            ref="upload"
            :limit="1"
            accept=".xlsx, .xls"
            :headers="upload.headers"
            :action="upload.url + '?profTpCd=20&updateSupport=' + upload.updateSupport"
            :disabled="upload.isUploading"
            :on-progress="handleFileUploadProgress"
            :on-success="handleFileSuccess"
            :auto-upload="false"
            drag
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip text-center" slot="tip">
              <div class="el-upload__tip" slot="tip">
                <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
              </div>
              <span>仅允许导入xls、xlsx格式文件。</span>
              <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
            </div>
          </el-upload>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitFileForm">确 定</el-button>
          <el-button @click="upload.open = false">取 消</el-button>
        </div>
      </el-dialog>
    </el-row>
  </div>
</template>

<script>
import {findPageList, saveOrUpdate, findById, deleteById} from "@/api/vehicleMng/transportVehicles";
import { getToken } from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { validateSfz,validatePhoneNumber } from '@/utils/validate'

export default {
  name: "perBase",
  dicts: ['fuel_type','audit_status','using_property','emission_level'],
  components: { Treeselect },
  data() {
    return {

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目表格数据
      baseList: [],
      // 用户列表
      userList:[],
      // 工种下拉框
      profBaseList:[],
      // 项目
      prjBaseList:[],
      // 单位
      untCpyList:[],
      // 班组
      untCpyGrpList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        emissionLevel:undefined,
        carCode:undefined
      },
      // 表单参数
      form: {
        id:undefined,
        carCode:undefined,
        fuelType:undefined,
        vin:undefined,
        engineCode:undefined,
        usingProperty:undefined,
        addr:undefined,
        userName:undefined,
        brandName:undefined,
        emissionLevel:undefined,
      },
      // 表单校验
      rules: {
        carCode: [
          {required: true, message: "车牌号不能为空", trigger: "change"}
        ],
        fuelType: [
          {required: true, message: "燃料类型不能为空", trigger: "change"}
        ],
        vin: [
          {required: true, message: "车辆识别码不能为空", trigger: "blur"}
        ],
        engineCode: [
          {required: true, message: "发动机编码不能为空", trigger: "blur"}
        ],
        usingProperty: [
          {required: true, message: "使用性质不能为空", trigger: "change"}
        ],
        addr: [
          {required: true,  message: "地址不能为空",trigger: "blur"}
        ],
        userName: [
          {required: true, message: "所有人不能为空", trigger: "change"}
        ],
        brandName: [
          {required: true, message: "品牌名称不能为空", trigger: "change"}
        ],
        emissionLevel: [
          {required: true, message: "排放阶段不能为空", trigger: "change"}
        ]
      },

      // 导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/person/perBase/importData"
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      findPageList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.baseList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange=[];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增运输车辆信息";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      let param = {id: row.id}
      findById(param).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改运输车辆";
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let param = Object.assign({profTpCd:"20"},this.form);
          saveOrUpdate(param).then(response => {
            this.$modal.msgSuccess("操作成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      let params = {idArray: ids}
      this.$modal.confirm('是否确认删除数据项？').then(function () {
        return deleteById(params);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

    /** 导出按钮操作 */
    handleExport() {
      let param = Object.assign({}, this.queryParams);
      this.download('door/vehicles/export', param, `运输车辆信息库_${new Date().getTime()}.xlsx`)
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "导入";
      this.upload.open = true;
    },

    /** 下载模板操作 */
    importTemplate() {
      this.download('person/perBase/importTemplate?profTpCd=20', {
      }, `劳务人员导入模板_${new Date().getTime()}.xlsx`)
    },

    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
